package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.text.TextUtils
import android.view.View
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.adapter.ManagerViewPageAdapter
import cc.vv.btpartyjob.partymanager.delegate.DiYiShuJiActivityDelegate
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.fragment.WorkGoingFragment
import cc.vv.btpartyjob.partymanager.fragment.WorkInFragment
import cc.vv.btpartyjob.partymanager.inter.AddWorkIdInterface
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 第一书记
 * Created by Chen.qi on 2018/10/29.
 */
class DiYiShuJiActivity : BaseActivityMVP<DiYiShuJiActivityDelegate>(), AddWorkIdInterface {


    private var addDetailId: String? = null

    /**
     * ViewPager适配器
     */
    private var mAdapter: ManagerViewPageAdapter? = null

    /**
     * Fragment集合
     */
    private var mFragments: ArrayList<Fragment>? = null

    /**
     * 村id
     */
    private var id: String? = null

    private var workGoingFragment: WorkGoingFragment? = null

    private var mineMobile: String? = null

    override fun initData() {
        viewDelegate?.setTopBarTitle("第一书记")
        id = intent.getStringExtra("id")
        mFragments = ArrayList()
        val workInFragment = WorkInFragment()
        workInFragment.setAddDetailInter(this)
        workInFragment.setCunId(id)
        workGoingFragment = WorkGoingFragment()
        mFragments!!.add(workInFragment)
        mFragments!!.add(workGoingFragment!!)
        mAdapter = ManagerViewPageAdapter(supportFragmentManager, mFragments!!)
        viewDelegate?.setAdapterData(mAdapter!!)
        viewDelegate?.setSocialSelected(true)
        viewDelegate?.setRightIcon(false)
    }

    override fun getDetailId(id: String?) {
        addDetailId = id
        workGoingFragment?.setCunId(id)
    }

    override fun setMobile(mobile: String?) {
        mineMobile = mobile
    }


    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
                val intent = Intent()
                if (!TextUtils.isEmpty(addDetailId) && UserInfoManageUtil.isEqualMobile(mineMobile)) {
                    intent.setClass(this@DiYiShuJiActivity, AddWorkQingKuangActivity::class.java)
                    intent.putExtra("id", addDetailId)
                    startActivityForResult(intent, 100)
                } else {
                    LKToastUtil.showToastShort("您不可进行该操作")
                }
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //第一书记
            viewDelegate?.setSocialSelected(true)
            viewDelegate?.setViewPagerPos(0)
            viewDelegate?.setRightIcon(false)

        }, R.id.tv_dysJ_jobQiaDao)
        viewDelegate?.setOnClickListener(View.OnClickListener {
            //工作情况
            viewDelegate?.setSocialSelected(false)
            viewDelegate?.setViewPagerPos(1)
            viewDelegate?.setRightIcon(true)
        }, R.id.tv_dysJ_jobQingKuang)

        viewDelegate?.bindViewPagerListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                if (position == 0) {
                    viewDelegate?.setRightIcon(false)
                    viewDelegate?.setSocialSelected(true)
                } else {
                    if (UserInfoManageUtil.isEqualMobile(mineMobile)) {
                        viewDelegate?.setRightIcon(true)
                    } else {
                        viewDelegate?.setRightIcon(false)
                    }
                    viewDelegate?.setSocialSelected(false)
                }
            }

        })

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == 101) {
            try {
                val workGoing = mFragments!![1] as WorkGoingFragment
                workGoing?.upData()
            } catch (e: Exception) {
            }
        }
    }

    override fun getDelegateClass(): Class<DiYiShuJiActivityDelegate> {
        return DiYiShuJiActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }


}